/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#pragma once

class CBrowseFolderDlg : public CWnd
{
  // Construction
public:
  // Construct a CBrowseFolderDlg dialog
  CBrowseFolderDlg(const CString &StrHint = CString(), UINT nFlags = 0, bool IsRecurse = false,
          CWnd *pParentWnd = NULL,
          int nRootFolder = CSIDL_DESKTOP);
  // Load the "hint" from Resource
  CBrowseFolderDlg(int Hint, UINT nFlags = 0, bool IsRecurse = false, CWnd *pParentWnd = NULL,
          int nRootFolder = CSIDL_DESKTOP);
  // Construct a CBrowseFolderDlg dialog rooted at an arbitrary folder
  CBrowseFolderDlg(const CString &StrHint, const CString &StrRoot,
          UINT nFlags = 0, bool IsRecurse = false, CWnd *pParentWnd = NULL);
  // Load the "hint" from Resource, but root the browse
  // operation at an arbitrary folder
  CBrowseFolderDlg(int nHint, const CString &StrRoot, UINT nFlags = 0, bool IsRecurse = false,
          CWnd *pParentWnd = NULL);
  // Deconstructor
  virtual ~CBrowseFolderDlg();

  // Attributes
public:
  // Retrieve the display name of the selected item
  CString GetDisplayName() const;
  // Retrieve the path to the selected item
  CString GetPath() const;
  // Determine whether the user checked "Recurse"
  bool GetRecurse() const;
  // Operations
public:
  // Display the Dialog - returns IDOK or IDCANCEL
  int DoModal();
  // En/Dis-able the "Ok" button
  void EnableOk(BOOL Enable);
  // Set the current selection in the tree control
  void SetSelection(LPCITEMIDLIST pItemIdList);
  // Set the current selection in the tree control
  void SetSelection(const CString &StrPath);
  // Set the "hint" text
  void SetText(const CString &StrNewText);

  // Overrides
public:
  // Called when the BFF Dialog has initialized
  virtual void OnInitBFFDialog();
  // Called when the selection has changed in the tree control
  virtual void OnBFFSelChanged(LPITEMIDLIST pNewSel);
  // Called when the user types an invalid name
  virtual BOOL OnBFFValidateFailed(const CString &StrBadName);
  // Process message
  virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,
                        AFX_CMDHANDLERINFO* pHandlerInfo);

  // Generated message map functions
protected:
  //{{AFX_MSG(CBrowseFolderDlg) NOTE - the ClassWizard will add and remove
  // member functions here.
  //}}AFX_MSG
  virtual void DoDataExchange(CDataExchange* pDX);
  afx_msg void OnDestroy();

  DECLARE_MESSAGE_MAP()

private:
  // Static method to be used for the SHBFF callback
  static int CALLBACK BrowseCallbackProc(HWND hWnd, UINT nMsg,
                                         LPARAM lParam,
                                         LPARAM lpData);
  // CSIDL => ITEMIDLIST
  LPITEMIDLIST ResolveCsidl(int nCsidl) const;
  // Textual filesystem path => ITEMIDLIST
  LPITEMIDLIST ResolveFsPath(const CString &strPath) const;

private:
  CString m_DisplayName;      // Display name of the selected item
  UINT m_Flags;               // Flags to be passed to the browse dialog
  CString m_HintTip;          // "Hint" to be displayed above the tree control
  CWnd *m_ParentWnd;          // Parent CWnd (NULL => App main window)
  CString m_Path;             // Selected path
  LPITEMIDLIST m_RootFolder;  // ITEMIDLIST identifying the root
  CButton m_RecurseChkBox;    // Checkbox we'll be adding
  BOOL m_IsRecurse;           // Recurse setting
};